package com.powernode.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.powernode.entity.SysRole;
import com.powernode.entity.SysUser;
import com.powernode.model.Result;
import com.powernode.service.SysRoleService;
import com.powernode.util.AuthUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import jdk.internal.org.objectweb.asm.Handle;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 系统角色管理控制层
 */
@Api(tags = "系统角色接口管理")
@RestController
@RequestMapping("sys/role")
public class SysRoleController {

    @Autowired
    private SysRoleService sysRoleService;

    @ApiOperation("查询系统所有角色")
    @GetMapping("list")
    @PreAuthorize("hasAnyAuthority('sys:role:list')")
    public Result<List<SysRole>> loadSysRoleList(){

        List<SysRole> roleList = sysRoleService.querySysRoleList();

        return Result.success(roleList);
    }



    @ApiOperation("多条件分页查询角色列表")
    @GetMapping("page")
    @PreAuthorize("hasAnyAuthority('sys:role:page')")
    public Result<Page<SysRole>> loadSysRolePage(@RequestParam Long current,
                                                 @RequestParam Long size,
                                                 @RequestParam(required = false) String roleName){

        Page<SysRole> page = new Page<>(current,size);

        page = sysRoleService.page(page,new LambdaQueryWrapper<SysRole>()
                .like(StrUtil.isNotBlank(roleName),SysRole::getRoleName,roleName)
                .orderByDesc(SysRole::getCreateTime));

        return Result.success(page);
    }

    @ApiOperation("新增角色")
    @PostMapping
    @PreAuthorize("hasAnyAuthority('sys:role:save')")
    public Result<String> saveSysRole(@RequestBody SysRole sysRole){
        Boolean saved = sysRoleService.saveSysRole(sysRole);
        return Result.handle(saved);

    }

    @ApiOperation("修改角色信息")
    @PostMapping
    @PreAuthorize("hasAnyAuthority('sys:role:update')")
    public Result<String> modifySysRole(@RequestBody SysRole sysRole){
        Boolean updated = sysRoleService.modifySysRole(sysRole);
        return Result.handle(updated);

    }

    @ApiOperation("批量/单个删除角色")
    @DeleteMapping
    @PreAuthorize("hasAnyAuthority('sys:role:delete')")
    public Result<String> removeSysRole(@RequestBody List<Long> roleIdList){
        Boolean removed = sysRoleService.removeSysRoleListByIds(roleIdList);
        return Result.handle(removed);

    }

}
